<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer></script>
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }

        .container {
            max-width: 800px;
            margin-top: 50px;
        }

        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }

        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            border-color: #545b62;
        }

        .form-control:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(38, 143, 255, 0.25);
        }
    </style>
</head>
<body>

<div class="container" x-data="editContactForm()">
    <div class="card">
        <h1>Edit Contact</h1>

        <!-- Edit Contact Form -->
        <form @submit.prevent="submitForm">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" x-model="contact.name" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" x-model="contact.phoneNumber" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" x-model="contact.email" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" x-model="contact.school" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" x-model="contact.gender">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" x-model="contact.birthYear" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" x-model="contact.major" />
            </div>

            <div class="form-group text-center">
                <button type="submit" class="btn btn-primary">Save</button>
                <a href="javascript:history.back()" class="btn btn-secondary ml-3">Cancel</a>
            </div>
        </form>
    </div>
</div>

<script>
    function editContactForm() {
        return {
            contact: {
                name: '',
                phoneNumber: '',
                email: '',
                school: '',
                gender: 'Male',
                birthYear: '',
                major: '',
            },
            init() {
                const params = new URLSearchParams(window.location.search);
                const id = params.get('id'); // Extract ID from URL

                fetch(`/contacts/get?id=${id}`)
                    .then(response => response.json())
                    .then(data => {
                        this.contact = data;
                    })
                    .catch(error => console.error('Error fetching contact data:', error));
            },
            submitForm() {
                const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                if (!emailRegex.test(this.contact.email)) {
                    alert('Please enter a valid email address.');
                    return;
                }

                const phoneRegex = /^1[3-9]\d{9}$/;
                if (!phoneRegex.test(this.contact.phoneNumber)) {
                    alert('Please enter a valid phone number.');
                    return;
                }

                fetch('/contacts/save', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(this.contact),
                })
                    .then(response => {
                        if (response.ok) {
                            alert('Contact updated successfully.');
                            window.history.back();  // Go back to the previous page
                        } else {
                            alert('Failed to update contact.');
                        }
                    })
                    .catch(error => console.error('Error submitting form:', error));
            },
        };
    }
</script>

</body>
</html>

<!--<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js" defer></script>
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }

        .container {
            max-width: 800px;
            margin-top: 50px;
        }

        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }

        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            border-color: #545b62;
        }

        .form-control:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(38, 143, 255, 0.25);
        }
    </style>
</head>
<body>

<div class="container" x-data="editContactForm()">
    <div class="card">
        <h1>Edit Contact</h1>

        &lt;!&ndash; Edit Contact Form &ndash;&gt;
        <form @submit.prevent="submitForm">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" x-model="contact.name" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" x-model="contact.phoneNumber" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" x-model="contact.email" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" x-model="contact.school" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" x-model="contact.gender">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" x-model="contact.birthYear" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" x-model="contact.major" />
            </div>

            <div class="form-group text-center">
                <button type="submit" class="btn btn-primary">Save</button>
                <a href="javascript:history.back()" class="btn btn-secondary ml-3">Cancel</a>
            </div>
        </form>
    </div>
</div>

<script>
    function editContactForm() {
        return {
            contact: {
                name: '',
                phoneNumber: '',
                email: '',
                school: '',
                gender: 'Male',
                birthYear: '',
                major: '',
            },
            init() {
                const params = new URLSearchParams(window.location.search);
                const id = params.get('id'); // Extract ID from URL

                fetch(`/contacts/get?id=${id}`)
                    .then(response => response.json())
                    .then(data => {
                        this.contact = data;
                    })
                    .catch(error => console.error('Error fetching contact data:', error));
            },
            submitForm() {
                const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
                if (!emailRegex.test(this.contact.email)) {
                    alert('Please enter a valid email address.');
                    return;
                }

                const phoneRegex = /^1[3-9]\d{9}$/;
                if (!phoneRegex.test(this.contact.phoneNumber)) {
                    alert('Please enter a valid phone number.');
                    return;
                }

                fetch('/contacts/save', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(this.contact),
                })
                    .then(response => {
                        if (response.ok) {
                            alert('Contact updated successfully.');
                            window.location.href = '/contacts';
                        } else {
                            alert('Failed to update contact.');
                        }
                    })
                    .catch(error => console.error('Error submitting form:', error));
            },
        };
    }
</script>

</body>
</html>-->


<!--
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Contact</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f7fc;
            font-family: 'Arial', sans-serif;
        }

        .container {
            max-width: 800px;
            margin-top: 50px;
        }

        .card {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-control {
            border-radius: 5px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }

        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            border-color: #545b62;
        }

        .form-control:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(38, 143, 255, 0.25);
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">
        <h1>Edit Contact</h1>

        &lt;!&ndash; Edit Contact Form &ndash;&gt;
        <form id="contactForm" th:action="@{/contacts/save}" th:object="${contact}" method="post">
            <input type="hidden" th:field="*{id}" />

            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" class="form-control" th:field="*{name}" required />
            </div>

            <div class="form-group">
                <label for="phoneNumber">Phone Number</label>
                <input type="text" id="phoneNumber" class="form-control" th:field="*{phoneNumber}" required />
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" class="form-control" th:field="*{email}" required />
            </div>

            <div class="form-group">
                <label for="school">School</label>
                <input type="text" id="school" class="form-control" th:field="*{school}" />
            </div>

            <div class="form-group">
                <label for="gender">Gender</label>
                <select id="gender" class="form-control" th:field="*{gender}">
                    <option value="Male" th:selected="${contact.gender == 'Male'}">Male</option>
                    <option value="Female" th:selected="${contact.gender == 'Female'}">Female</option>
                </select>
            </div>

            <div class="form-group">
                <label for="birthYear">Birth Year</label>
                <input type="number" id="birthYear" class="form-control" th:field="*{birthYear}" />
            </div>

            <div class="form-group">
                <label for="major">Major</label>
                <input type="text" id="major" class="form-control" th:field="*{major}" />
            </div>

            <div class="form-group text-center">
                <button type="submit" class="btn btn-primary">Save</button>
                <a href="javascript:history.back()" class="btn btn-secondary ml-3">Cancel</a>
            </div>
        </form>
    </div>
</div>

&lt;!&ndash; JS imports &ndash;&gt;
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
    // 表单提交前的验证
    document.getElementById("contactForm").addEventListener("submit", function(event) {
        // 获取表单输入内容
        var email = document.getElementById("email").value;
        var phoneNumber = document.getElementById("phoneNumber").value;

        // 邮箱正则表达式（基本验证）
        var emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

        // 中国大陆手机号正则表达式（以1开头，第二位为3-9，后面9位数字）
        var phoneRegex = /^1[3-9]\d{9}$/;

        // 验证邮箱格式
        if (!emailRegex.test(email)) {
            alert("Please enter a valid email address.");
            event.preventDefault(); // 阻止表单提交
            return;
        }

        // 验证手机号格式
        if (!phoneRegex.test(phoneNumber)) {
            alert("Please enter a valid phone number.");
            event.preventDefault(); // 阻止表单提交
            return;
        }
    });
</script>

</body>
</html>

-->
